jun.TransaksiBahanGrid = Ext.extend(Ext.grid.GridPanel, {
    title: "Transaksi Bahan",
    id: 'docs-jun.TransaksiBahanGrid',
    iconCls: 'silk-grid',
    viewConfig: {
        forceFit: true
    },
    sm: new Ext.grid.RowSelectionModel({
        singleSelect: true
    }),
    columns: [{
            header: 'Tanggal Transaksi',
            sortable: true,
            resizable: true,
            dataIndex: 'tgl_trans',
            width: 100
        }, {
            header: 'Ref. Dokumen',
            sortable: true,
            resizable: true,
            dataIndex: 'ref',
            width: 100
        }, {
            header: 'Nama Barang',
            sortable: true,
            resizable: true,
            dataIndex: 'nama_barang',
            width: 100
        }, {
            header: 'Jumlah',
            sortable: true,
            resizable: true,
            dataIndex: 'jml',
            width: 100
        }, {
            header: 'Harga Satuan',
            sortable: true,
            resizable: true,
            dataIndex: 'harga_satuan',
            width: 100
        }, {
            header: 'Total',
            sortable: true,
            resizable: true,
            dataIndex: 'total',
            width: 100
        }, {
            header: 'Kavling',
            sortable: true,
            resizable: true,
            dataIndex: 'id_kavling',
            width: 100
        }

    ],
    initComponent: function() {
        this.store = jun.rztTransaksiBahan;
        this.tbar = {
            xtype: 'toolbar',
            items: [{
                    xtype: 'button',
                    text: 'Tambah Transaksi Bahan',
                    ref: '../btnAdd'
                }, {
                    xtype: 'tbseparator'

                }, {
                    xtype: 'button',
                    text: 'Lihat Transaksi Bahan',
                    ref: '../btnEdit'
                }, {
                    xtype: 'tbseparator'

                }, {
                    xtype: 'button',
                    text: 'Void Transaksi Bahan',
                    ref: '../btnDelete'
                }]
        };
        jun.rztTransaksiBahan.reload();
        jun.rztBank.reload();
        jun.rztKavling.reload();
        jun.TransaksiBahanGrid.superclass.initComponent.call(this);
        this.btnAdd.on('Click', this.loadForm, this);
        this.btnEdit.on('Click', this.loadEditForm, this);
        this.btnDelete.on('Click', this.deleteRec, this);
        this.getSelectionModel().on('rowselect', this.getrow, this);
    },
    getrow: function(sm, idx, r) {
        this.record = r;
        var selectedz = this.sm.getSelections();
    },
    loadForm: function() {
        var form = new jun.TransaksiBahanWin({
            modez: 0
        });
        form.show();
    },
    loadEditForm: function() {
        var selectedz = this.sm.getSelected();
        if (selectedz == "") {
            Ext.MessageBox.alert("Warning", "Anda belum memilih transaksi bahan.");
            return;
        }
        var idz = selectedz.json.id_transaksi_bahan;
        Ext.Ajax.request({
            url: 'TransaksiBahan/view',
            params: {
                id: idz,
            },
            method: 'POST',
            success: function(f, a) {
                var response = Ext.decode(f.responseText);
                if (response.success == false) {
                    Ext.MessageBox.show({
                        title: 'Warning',
                        msg: response.msg,
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.WARNING
                    });
                    return;
                }
                var data = response.results[0];
                var form = new jun.TransaksiBahanShowWin({
                    modez: 1,
                    id: idz
                });
                form.txtRef.text = data.ref;
                form.trans_date.text = data.tgl_trans;
                form.barang.text = data.nama_barang;
                form.harga_satuan.text = Ext.util.Format.number(data.harga_satuan, '0,0');;
                form.jml.text = Ext.util.Format.number(data.jml, '0,0');;                
                form.total.text = Ext.util.Format.number(data.total, '0,0');;                
                form.bank.text = data.nama_akun;
                form.kavling.text = data.nama;
//                form.amount.text = Ext.util.Format.number(data.amount, '0,0');
//                form.trans_via.text = data.trans_via;
//                form.note.text = data.note;
                form.show(this);
            },
            failure: function(f, a) {
                Ext.MessageBox.alert("Error", "Can't Communicate With The Server");
            }
        });
    },
    deleteRec: function() {
        Ext.MessageBox.confirm('Pertanyaan',
                'Apakah anda yakin ingin menghapus data ini?',
                this.deleteRecYes, this);
    },
    deleteRecYes: function(btn) {
        if (btn == 'no') {
            return;
        }
        var record = this.sm.getSelected();
        // Check is list selected
        if (record == "") {
            Ext.MessageBox.alert("Warning", "Anda Belum Memilih Data");
            return;
        }
        var idz = record.json.id_transaksi_bahan;
        var form = new jun.TransaksiBahanVoidWin({
            id: idz
        });
        form.show(this);
    }
})